package com.xuhuan.demo.controller;

import com.xuhuan.demo.vo.UserVO;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.stream.Collectors;

/**
 * @author: 徐欢
 * @date: 2021/8/4 14:52
 * @description: 用户信息控制器
 */
@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * 校验用户信息是否规范
     *
     * @param user
     * @param bindingResult
     * @return
     */
    @PostMapping("/check")
    public String checkUser(@RequestBody @Valid UserVO user, BindingResult bindingResult) {
        // 参数校验
        if (bindingResult.hasErrors()) {
            return bindingResult.getAllErrors()
                    .stream()
                    .map(ObjectError::getDefaultMessage)
                    .collect(Collectors.joining(","));
        }
        return "";
    }
}
